﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WarOfDev.Web.Models.MappingToViewModel;
using WarOfDev.Web.ViewModels;
using WarOfDev.Web.ViewModels.MappingToModel;
using WarOfDev.Core.ActionFilters;
using WarOfDev.Core.ExceptionHandling.Attributes;

namespace WarOfDev.Web.Controllers
{
    public class DeveloperController : Controller
    {
        private EF.WarDevEntities db = new EF.WarDevEntities();

      

        //
        // GET: /Developer/
        [CustomAuthorize("UserAdmin.ShowDevelopers")]
        [ViewHandleError(IsPartial = false)]
        public ActionResult Index()
        {
            return View();
        }

        //public ActionResult DevelopersRead([DataSourceRequest] DataSourceRequest request)
        //{
        //    return Json(GetDevelopers().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        //}

        //public ActionResult GetTasksByDevId([DataSourceRequest] DataSourceRequest request, string devId)
        //{
        //    var developer = db.Developers.FirstOrDefault(d => d.Id == devId);
        //    var tasks = developer.Tasks.ToList().Select(t => t.Task.ToViewModel()).ToList();

        //    return Json(tasks.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        //}

        private IEnumerable<DeveloperViewModel> GetDevelopers()
        {
            return db.Developers.AsEnumerable().Select(d => d.ToViewModel()).ToList();
        }

        //
        // GET: /Developer/Details/5

        public ActionResult Details(string id)
        {
            EF.Developer developer = db.Developers.Single(d => d.Id == id);
            if (developer == null)
            {
                return HttpNotFound();
            }
            return View(developer);
        }

        //
        // POST: /Developer/Create

        //[AcceptVerbs(HttpVerbs.Get)]
        //public ActionResult Create([DataSourceRequest] DataSourceRequest request, DeveloperViewModel developer)
        //{

        //    if (ModelState.IsValid)
        //    {
        //        developer.Id = Guid.NewGuid().ToString();
        //        developer.CreatedBy = Guid.NewGuid().ToString();
        //        developer.CreatedOn = DateTime.Now;
        //        developer.ModifiedBy = Guid.NewGuid().ToString();
        //        developer.ModifiedOn = DateTime.Now;



        //        var dev = new EF.Developer { Id = developer.Id };
        //        db.Developers.AddObject(dev);
        //        developer.ToModel(dev);
        //        db.SaveChanges();
        //        db.Refresh(System.Data.Objects.RefreshMode.ClientWins, db.Developers);
        //    }

        //    return Json(new[] { developer }.ToDataSourceResult(request, ModelState));
        //}

        //
        // GET: /Developer/Edit/5
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string id)
        {
            EF.Developer developer = db.Developers.Single(d => d.Id == id);
            if (developer == null)
            {
                return HttpNotFound();
            }
            return View(developer);
        }

        ////
        //// POST: /Developer/Edit/5
        //[AcceptVerbs(HttpVerbs.Get)]
        //public ActionResult Edit([DataSourceRequest] DataSourceRequest request, DeveloperViewModel developer)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        developer.ModifiedOn = DateTime.Now;

        //        var dev = new EF.Developer { Id = developer.Id };
        //        db.Developers.Attach(dev);
        //        developer.ToModel(dev);
        //        db.SaveChanges();
        //        db.Refresh(System.Data.Objects.RefreshMode.ClientWins, dev);
        //    }

        //    return Json(new[] { developer }.ToDataSourceResult(request, ModelState));
        //}

        ////
        // GET: /Developer/Delete/5
        public ActionResult Delete(string id)
        {
            EF.Developer developer = db.Developers.Single(d => d.Id == id);
            if (developer == null)
            {
                return HttpNotFound();
            }
            return View(developer);
        }

        //
        // POST: /Developer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            EF.Developer developer = db.Developers.Single(d => d.Id == id);
            db.Developers.DeleteObject(developer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


    }
}
